﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CreatureSoft.Hoot.Service.Security;
using CreatureSoft.Hoot.Web.Security;
using CreatureSoft.Hoot.Web.Models;

namespace CreatureSoft.Hoot.Web.Controllers
{
    public class AccountController : Controller
    {
        private readonly IHootMembershipProvider membershipProvider;

        public AccountController(IHootMembershipProvider membershipProvider)
        {
            this.membershipProvider = membershipProvider;
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel logOnModel)
        {
            var model = new BaseModel();
            if (membershipProvider.ValidateUser(logOnModel.UserName, logOnModel.Password))
            {
                //FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                //if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                //    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
               // {
                    //return Redirect(returnUrl);
                   // model.RedirectTo = returnUrl;
                //}
                //else
               // {

                model.RedirectTo = Url.Action("Index", "Home"); 
                        //UrlHelper.GenerateContentUrl("Home/Index", this.HttpContext) ;
                    //return RedirectToAction("Index", "Home");
                //}
            }
            else
            {
                //ModelState.AddModelError("", "The user name or password provided is incorrect.");
                model.Errors.Add("The user name or password provided is incorrect.");                
            }            

            // If we got this far, something failed, redisplay form
            return Json(model);
        }

        public ActionResult Login()
        {
            return View();
        }

    }
}
